package com.imay.live.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * 左对齐，然后 右边换行对齐，解决自动换行不整齐问题
 * lzx
 */
public class AlignTextView extends TextView {
    private float textSize;
    private int textColor;
    private int paddingRight;
    private int paddingLeft;
    private Paint paint;
    private float LineSpacing = 0f;
    int height;

    public AlignTextView(Context context) {
        super(context);
        init(context);
    }

    public AlignTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AlignTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = 21)
    public AlignTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }


    void init(Context context) {
        textSize = getTextSize();
        LineSpacing = getLineSpacingExtra();
        textColor = getCurrentTextColor();
        paddingLeft = getPaddingLeft();
        paddingRight = getPaddingRight();
        if (paint == null) {
            paint = getPaint();
            paint.setAntiAlias(true);
            paint.setColor(textColor);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float textShowWidth = canvas.getWidth() - paddingRight - paddingLeft;
        int lineCount = 1;
        if (TextUtils.isEmpty(this.getText())) {
            return;
        }
        String text = this.getText().toString();
        char[] textCharArray = text.toCharArray();
        float drawedWidth = 0;
        float charWidth = paint.measureText(textCharArray, 0, 1);
        for (int i = 0; i < textCharArray.length; i++) {
            if (textCharArray[i] == '\n') {
                lineCount++;
                drawedWidth = 0;
                continue;
            }
            if (textShowWidth - drawedWidth < charWidth) {
                lineCount++;
                drawedWidth = 0;
            }
            canvas.drawText(textCharArray, i, 1, drawedWidth + paddingLeft,
                    (lineCount) * textSize + (LineSpacing * (lineCount - 1)), paint);
            drawedWidth += charWidth;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int w = MeasureSpec.getSize(widthMeasureSpec);
        int h = 0;
        int lineCount = 1;
        float textShowWidth = w - paddingRight - paddingLeft;
        String text = this.getText().toString();
        if (TextUtils.isEmpty(text)) {
            text = " ";
        }
        char[] textCharArray = text.toCharArray();
        float drawedWidth = 0;
        float charWidth = paint.measureText(textCharArray, 0, 1);
        for (int i = 0; i < textCharArray.length; i++) {
            if (textCharArray[i] == '\n') {
                lineCount++;
                drawedWidth = 0;
                continue;
            }
            if (textShowWidth - drawedWidth < charWidth) {
                lineCount++;
                drawedWidth = 0;
            }
            h = (int) ((lineCount) * textSize + (LineSpacing * (lineCount - 1)));
            drawedWidth += charWidth;
        }
        h = (int) (textSize + LineSpacing + h);
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY));
    }
}
